package com.wing.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileUtils {

	/**
	 * 路径存在则覆盖，不存在则创建。
	 * 
	 * @param path
	 *            路径
	 * @param content
	 *            内容
	 * @throws IOException
	 */
	public static void saveFile(String path, String content) throws IOException {
		//System.out.println("save-------------------"+path);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		if (!file.getParentFile().exists()) {
			mkdir(file.getParentFile());
		}
		BufferedWriter bw = null;
		try {
			file.createNewFile();
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
			bw.write(new String(content.getBytes()));// 输出字符串
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 路径存在则覆盖，不存在则创建。
	 * 
	 * @param path
	 *            路径
	 * @param content
	 *            内容
	 * @throws IOException
	 */
	public static void saveFile(String path, byte[] content) throws IOException {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		if (!file.getParentFile().exists()) {
			mkdir(file.getParentFile());
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(path);
			fos.write(content); // 写入文件
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 递归创建目录
	 * 
	 * @param file
	 */
	public static void mkdir(File file) {
		if (file.exists()) {
			return;
		}
		if (!file.getParentFile().exists()) {
			mkdir(file.getParentFile());
		}
		file.mkdir();
	}

	/**
	 * 路径存在则覆盖，不存在则创建。
	 * 
	 * @param path
	 *            路径
	 * @param content
	 *            内容
	 * @throws IOException
	 */
	public static void deleteFile(String path) {
		//System.out.println("delete-------------------"+path);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	public static String getFileString(String path) throws IOException {
		File file = new File(path);
		if (!file.exists() || file.isDirectory()) {
			return "";
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
		String b;
		StringBuffer sb = new StringBuffer();
        int len=0;

		while ((b = br.readLine()) != null) {
			
			if(len != 0)  // 处理换行符的问题
            {
                sb.append("\r\n"+new String(b.getBytes()));
            }
            else
            {
                sb.append(new String(b.getBytes()));
            }
            len++;
		}
		return sb.toString();
	}

	public static void deleteDir(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	// /* 复制整个文件夹内容
	// *
	// * @param oldPath
	// * String 原文件路径 如：c:/old
	// * @param newPath
	// * String 复制后路径 如：f:/new
	// * @return boolean
	// */
	public static void copyDirectory(String oldPath, String newPath,String rodmomCPId) {

		try {
			(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
				}
				boolean updateDflag = false;
				if (temp.getAbsolutePath().indexOf("document") != -1){
					updateDflag = true;
					if(temp.getAbsolutePath().indexOf("siteimg")!= -1){updateDflag = false;}
					if(temp.getName().equals("document")){updateDflag = false;}
				}				
				if (temp.isFile()) {
					String newFileName = "";
					if (updateDflag) {
						newFileName = newPath + "/"+rodmomCPId
								+ (temp.getName()).toString();
					} else {
						newFileName = newPath + "/"
								+ (temp.getName()).toString();
					}
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newFileName);
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory()) {// 如果是子文件夹
					if (updateDflag) {
						copyDirectory(oldPath + "/" + file[i], newPath
								+ "/"+rodmomCPId + file[i],rodmomCPId);
					} else {
						copyDirectory(oldPath + "/" + file[i], newPath + "/"
								+ file[i],rodmomCPId);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("复制整个文件夹内容操作出错");
			e.printStackTrace();
		}
	}
}
